package internal

import (
	"MoSkeleton/framework"
	"MoSkeleton/framework/core/config/properties_gin"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/mo_gin/middleware/authentication"
	"MoSkeleton/framework/mo_gin/middleware/authorization"
	"MoSkeleton/framework/mo_gin/middleware/utils"
	au "MoSkeleton/web/middleware/authorize"
	"MoSkeleton/web/middleware/captcha_mw"
	"MoSkeleton/web/middleware/loginfo"
	"MoSkeleton/web/v1/http"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

type RouterMakeup struct {
	preHandlerChain  *utils.HandlerChain
	postHandlerChain *utils.HandlerChain
	authSubject      authorization.AuthSubject
}

func NewRouterMakeup(ginProperty *properties_gin.GinProperty) *RouterMakeup {
	router := new(RouterMakeup)
	router.authSubject = NewAuthSubject()
	router.preHandlerChain = utils.NewHandlerChain()
	//router.postHandlerChain = utils.NewHandlerChain()
	//router.postHandlerChain.Add(func(c *gin.Context) utils.ChainFlowContinue {
	//	global.MoLogger.Info("post handler: " + c.FullPath())
	//	return false
	//})
	router.make(ginProperty)
	return router
}

func (r *RouterMakeup) make(ginProperty *properties_gin.GinProperty) {
	contextPathGroup := r.makeContextPathGroup(ginProperty)
	privateGroup := contextPathGroup.Group("")
	publicGroup := contextPathGroup.Group("")
	var authorizeGroupHandlers = []gin.HandlerFunc{authentication.DoAuthGroup(), au.DoCheckBlackToken()}
	privateGroup.Use(authorizeGroupHandlers...)
	privateGroup.Use(r.preHandlerChain.DoChain())

	r.makeStatic(publicGroup, ginProperty)
	r.makeOtherRouter(publicGroup, ginProperty)
	r.makePublicPathGroup(publicGroup, ginProperty)
	r.makeActuatorPathGroup(publicGroup, privateGroup, ginProperty)

	r.makeSystemPathGroup(privateGroup, ginProperty)
	r.makeCommonPathGroup(privateGroup, ginProperty)
	r.makeMonitorPathGroup(privateGroup, ginProperty)
	r.makeToolPathGroup(privateGroup, ginProperty)

	// --------$%replenish gen code at down place: Make%$--------------
}

func (r *RouterMakeup) makeContextPathGroup(ginProperty *properties_gin.GinProperty) *gin.RouterGroup {
	contextPath := ginProperty.Router.ContextPath
	return framework.GinRouter.Group(contextPath)
}

func (r *RouterMakeup) makeSystemPathGroup(privateGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysPathGroup := r.createRouterGroup(ginProperty.Router.SystemGroupPath, "/system", privateGroup)
	sysPathGroup.POST("/logout", http.MainCtrlGroupInst.SystemCtrlGroupInst.SysAuthCtrlInst.Logout)
	//user
	r.makeSystemUserPathGroup(sysPathGroup, ginProperty)
	//menu
	r.makeSystemMenuPathGroup(sysPathGroup, ginProperty)
	//dict
	r.makeSystemDictPathGroup(sysPathGroup, ginProperty)
	//config
	r.makeSystemConfigPathGroup(sysPathGroup, ginProperty)
	//post
	r.makeSystemPostPathGroup(sysPathGroup, ginProperty)
	//dept
	r.makeSystemDeptPathGroup(sysPathGroup, ginProperty)
	//role
	r.makeSystemRolePathGroup(sysPathGroup, ginProperty)
	//notice
	r.makeSystemNoticePathGroup(sysPathGroup, ginProperty)

	//tool
}
func (r *RouterMakeup) makeCommonPathGroup(privateGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	commonPathGroup := r.createRouterGroup("", "/common", privateGroup)
	commonPathGroup.POST("/upload", http.MainCtrlGroupInst.CommonCtrlGroupInst.CommonCtrlInst.UploadFile)
}

func (r *RouterMakeup) makePublicPathGroup(publicGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	whitePathGroup := r.createRouterGroup(ginProperty.Router.PublicGroupPath, "/public", publicGroup)

	whitePathGroup.GET("/captchaImage", http.MainCtrlGroupInst.PublicCtrlGroupInst.CaptchaCtlInst.Captcha)
	whitePathGroup.POST("/login", loginfo.DoLogLoginInfo(), captcha_mw.DoCheckCaptcha(), http.MainCtrlGroupInst.PublicCtrlGroupInst.AuthCtrlInst.Login)
}
func (r *RouterMakeup) makeActuatorPathGroup(publicGroup *gin.RouterGroup, privateGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	actuatorPublicPathGroup := r.createRouterGroup("", "/actuator", publicGroup)
	actuatorPrivatePathGroup := r.createRouterGroup("", "/actuator", privateGroup)
	r.makeActuatorPublicPathGroup(actuatorPublicPathGroup, ginProperty)
	r.makeActuatorPrivatePathGroup(actuatorPrivatePathGroup, ginProperty)
}

func (r *RouterMakeup) makeMonitorPathGroup(privateGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	monitorPathGroup := r.createRouterGroup("/monitor", "/monitor", privateGroup)
	r.makeMonitorOperLogPathGroup(monitorPathGroup, ginProperty)
	r.makeMonitorLoginInfoPathGroup(monitorPathGroup, ginProperty)
}

func (r *RouterMakeup) makeActuatorPublicPathGroup(actuatorPublicPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	healthIndicatorCtrlInst := http.MainCtrlGroupInst.ActuatorCtrlGroupInst.HealthIndicatorCtrlInst
	actuatorPublicPathGroup.GET("/health", healthIndicatorCtrlInst.Health)
}

func (r *RouterMakeup) makeActuatorPrivatePathGroup(actuatorPrivatePathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {

}

// user
func (r *RouterMakeup) makeSystemUserPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysUserCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysUserCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysUserCtrlInst.InitCtrl(sysUserCtrlInst)}
	userPathGroup := r.createRouterGroup("/user", "/user", contextPathGroup)
	userPathGroup.GET("/getInfo", sysUserCtrlInst.GetLoginUserInfo)
	userPathGroup.GET("/:id", r.checkPermission("system:user:query"), sysUserCtrlInst.GetUserDetailById)
	userPathGroup.GET("/", r.checkPermission("system:user:query"), sysUserCtrlInst.GetUserDetailById)
	userPathGroup.GET("/list", r.checkPermission("system:user:list"), sysUserCtrlInst.GetUserPage)
	userPathGroup.GET("/deptTree", r.checkPermission("system:user:list"), sysUserCtrlInst.GetDeptTree)

	userPathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:add"), sysUserCtrlInst.AddUser)...)
	userPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:edit"), sysUserCtrlInst.UpdateUser)...)
	userPathGroup.PUT("/changeStatus", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:edit"), sysUserCtrlInst.ChangeStatus)...)
	userPathGroup.PUT("/resetPwd", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:resetPwd"), sysUserCtrlInst.ResetPassword)...)
	userPathGroup.DELETE("/:ids", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:remove"), sysUserCtrlInst.DeleteUser)...)
	userPathGroup.POST("/export", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:export"), sysUserCtrlInst.ExportUser)...)
	userPathGroup.POST("/importTemplate", r.mergeHandlers(extraHandlers, sysUserCtrlInst.ImportTemplate)...)
	userPathGroup.POST("/importData", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:import"), sysUserCtrlInst.ImportUser)...)
	userPathGroup.GET("/authRole/:id", r.checkPermission("system:user:query"), sysUserCtrlInst.GetAuthRole)
	userPathGroup.PUT("/authRole", r.mergeHandlers(extraHandlers, r.checkPermission("system:user:edit"), sysUserCtrlInst.UpdateAuthRole)...)
	userPathGroup.GET("/profile", sysUserCtrlInst.GetProfile)
	userPathGroup.PUT("/profile", r.mergeHandlers(extraHandlers, sysUserCtrlInst.UpdateProfile)...)
	userPathGroup.PUT("/profile/updatePwd", r.mergeHandlers(extraHandlers, sysUserCtrlInst.UpdateProfilePassword)...)
	userPathGroup.POST("/profile/avatar", r.mergeHandlers(extraHandlers, sysUserCtrlInst.UpdateProfileAvatar)...)

}

// menu
func (r *RouterMakeup) makeSystemMenuPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysMenuCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysMenuCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysMenuCtrlInst.InitCtrl(sysMenuCtrlInst)}
	menuPathGroup := r.createRouterGroup("/menu", "/menu", contextPathGroup)
	menuPathGroup.GET("/getRouters", sysMenuCtrlInst.GetRouters)
	menuPathGroup.GET("/list", r.checkPermission("system:menu:list"), sysMenuCtrlInst.GetMenuAll)
	menuPathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("system:menu:add"), sysMenuCtrlInst.AddMenu)...)
	menuPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("system:menu:edit"), sysMenuCtrlInst.UpdateMenuById)...)
	menuPathGroup.GET("/:id", r.checkPermission("system:menu:query"), sysMenuCtrlInst.GetMenuById)
	menuPathGroup.DELETE("/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:menu:remove"), sysMenuCtrlInst.DeleteById)...)
	menuPathGroup.GET("/treeselect", sysMenuCtrlInst.Treeselect)
	menuPathGroup.GET("/roleMenuTreeselect/:roleId", sysMenuCtrlInst.RoleMenuTreeselect)
}

// dict
func (r *RouterMakeup) makeSystemDictPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	dictTypeCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysDictTypeCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), dictTypeCtrlInst.InitCtrl(dictTypeCtrlInst)}
	dictPathGroup := r.createRouterGroup("/dict", "/dict", contextPathGroup)
	//type
	dictPathGroup.GET("/type/list", r.checkPermission("system:dict:list"), dictTypeCtrlInst.GetDataTypeList)
	dictPathGroup.GET("/type/:id", r.checkPermission("system:dict:query"), dictTypeCtrlInst.GetDataTypeById)
	dictPathGroup.GET("/type/optionselect", dictTypeCtrlInst.OptionSelect)
	dictPathGroup.PUT("/type", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:edit"), dictTypeCtrlInst.UpdateDataTypeById)...)
	dictPathGroup.POST("/type", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:add"), dictTypeCtrlInst.AddDataType)...)
	dictPathGroup.DELETE("/type/refreshCache", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:remove"), dictTypeCtrlInst.RefreshCache)...)
	dictPathGroup.DELETE("/type/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:remove"), dictTypeCtrlInst.DeleteDataTypeById)...)
	dictPathGroup.POST("/type/export", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:export"), dictTypeCtrlInst.ExportDataType)...)

	//data
	dictDataCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysDictDataCtrlInst
	extraHandlers = []gin.HandlerFunc{loginfo.DoLogOperInfo(), dictDataCtrlInst.InitCtrl(dictDataCtrlInst)}
	dictPathGroup.GET("/data/type/:dictType", dictDataCtrlInst.GetDataListByDictType)
	dictPathGroup.GET("/data/list", r.checkPermission("system:dict:list"), dictDataCtrlInst.GetDictDataPage)
	dictPathGroup.GET("/data/:id", r.checkPermission("system:dict:query"), dictDataCtrlInst.GetDictDataById)
	dictPathGroup.POST("/data", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:add"), dictDataCtrlInst.AddDictData)...)
	dictPathGroup.PUT("/data", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:edit"), dictDataCtrlInst.UpdateDictDataById)...)
	dictPathGroup.DELETE("/data/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:remove"), dictDataCtrlInst.DeleteById)...)
	dictPathGroup.POST("/data/export", r.mergeHandlers(extraHandlers, r.checkPermission("system:dict:export"), dictDataCtrlInst.ExportDictData)...)
}

// config
func (r *RouterMakeup) makeSystemConfigPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysConfigCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysConfigCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysConfigCtrlInst.InitCtrl(sysConfigCtrlInst)}
	configPathGroup := r.createRouterGroup("/config", "/config", contextPathGroup)
	configPathGroup.GET("/list", r.checkPermission("system:config:list"), sysConfigCtrlInst.GetConfigPage)
	configPathGroup.GET("/:id", r.checkPermission("system:config:query"), sysConfigCtrlInst.GetConfigById)
	configPathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("system:config:add"), sysConfigCtrlInst.AddConfig)...)
	configPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("system:config:edit"), sysConfigCtrlInst.UpdateConfigById)...)
	configPathGroup.DELETE("/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:config:remove"), sysConfigCtrlInst.DeleteById)...)
	configPathGroup.DELETE("/refreshCache", r.mergeHandlers(extraHandlers, r.checkPermission("system:config:remove"), sysConfigCtrlInst.RefreshCache)...)
	configPathGroup.GET("/configKey/:configKey", sysConfigCtrlInst.GetByConfigKey)
	configPathGroup.POST("/export", r.mergeHandlers(extraHandlers, r.checkPermission("system:config:export"), sysConfigCtrlInst.ExportConfig)...)
}

// post
func (r *RouterMakeup) makeSystemPostPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysPostCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysPostCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysPostCtrlInst.InitCtrl(sysPostCtrlInst)}
	postPathGroup := r.createRouterGroup("/post", "/post", contextPathGroup)
	postPathGroup.GET("/list", r.checkPermission("system:post:list"), sysPostCtrlInst.GetPostPage)
	postPathGroup.GET("/:id", r.checkPermission("system:post:query"), sysPostCtrlInst.GetPostById)
	postPathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("system:post:add"), sysPostCtrlInst.AddPost)...)
	postPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("system:post:edit"), sysPostCtrlInst.UpdatePostById)...)
	postPathGroup.DELETE("/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:post:remove"), sysPostCtrlInst.DeletePostById)...)
	postPathGroup.POST("/export", r.mergeHandlers(extraHandlers, r.checkPermission("system:post:export"), sysPostCtrlInst.ExportPost)...)
}

// dept
func (r *RouterMakeup) makeSystemDeptPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysDeptCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysDeptCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysDeptCtrlInst.InitCtrl(sysDeptCtrlInst)}
	deptPathGroup := r.createRouterGroup("/dept", "/dept", contextPathGroup)
	deptPathGroup.GET("/list", r.checkPermission("system:dept:list"), sysDeptCtrlInst.GetDeptAll)
	deptPathGroup.GET("/:id", r.checkPermission("system:dept:query"), sysDeptCtrlInst.GetDeptById)
	deptPathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("system:dept:add"), sysDeptCtrlInst.AddDept)...)
	deptPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("system:dept:edit"), sysDeptCtrlInst.UpdateDeptById)...)
	deptPathGroup.DELETE("/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:dept:remove"), sysDeptCtrlInst.DeleteById)...)
	deptPathGroup.GET("/list/exclude/:id", r.checkPermission("system:dept:list"), sysDeptCtrlInst.GetExcludeDeptAndChildren)
}

// role
func (r *RouterMakeup) makeSystemRolePathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysRoleCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysRoleCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysRoleCtrlInst.InitCtrl(sysRoleCtrlInst)}
	rolePathGroup := r.createRouterGroup("/role", "/role", contextPathGroup)
	rolePathGroup.GET("/list", r.checkPermission("system:role:list"), sysRoleCtrlInst.GetRolePage)
	rolePathGroup.GET("/:id", r.checkPermission("system:role:query"), sysRoleCtrlInst.GetRoleById)
	rolePathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:add"), sysRoleCtrlInst.AddRole)...)
	rolePathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:edit"), sysRoleCtrlInst.UpdateRoleById)...)
	rolePathGroup.PUT("/changeStatus", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:edit"), sysRoleCtrlInst.UpdateRoleStatus)...)
	rolePathGroup.DELETE("/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:remove"), sysRoleCtrlInst.DeleteById)...)
	rolePathGroup.GET("/deptTree/:id", r.checkPermission("system:role:query"), sysRoleCtrlInst.GetDeptTreeByRole)
	rolePathGroup.PUT("/dataScope", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:edit"), sysRoleCtrlInst.UpdateDataScope)...)
	rolePathGroup.POST("/export", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:export"), sysRoleCtrlInst.ExportRole)...)
	//authUser
	roleAuthUserCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.RoleAuthUserCtrlInst
	extraHandlers = []gin.HandlerFunc{loginfo.DoLogOperInfo(), roleAuthUserCtrlInst.InitCtrl(roleAuthUserCtrlInst)}
	authUserPathGroup := r.createRouterGroup("/authUser", "/authUser", rolePathGroup)
	authUserPathGroup.GET("/allocatedList", r.checkPermission("system:role:list"), roleAuthUserCtrlInst.GetAllocatedListPage)
	authUserPathGroup.GET("/unallocatedList", r.checkPermission("system:role:list"), roleAuthUserCtrlInst.GetUnallocatedListPage)
	authUserPathGroup.PUT("/selectAll", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:edit"), roleAuthUserCtrlInst.AuthUserAll)...)
	authUserPathGroup.PUT("/cancel", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:edit"), roleAuthUserCtrlInst.CancelAuthUser)...)
	authUserPathGroup.PUT("/cancelAll", r.mergeHandlers(extraHandlers, r.checkPermission("system:role:edit"), roleAuthUserCtrlInst.CancelAuthUserAll)...)
}

// notice
func (r *RouterMakeup) makeSystemNoticePathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysNoticeCtrlInst := http.MainCtrlGroupInst.SystemCtrlGroupInst.SysNoticeCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysNoticeCtrlInst.InitCtrl(sysNoticeCtrlInst)}
	noticePathGroup := r.createRouterGroup("/notice", "/notice", contextPathGroup)
	noticePathGroup.GET("/list", r.checkPermission("system:notice:list"), sysNoticeCtrlInst.GetNoticePage)
	noticePathGroup.GET("/:id", r.checkPermission("system:notice:query"), sysNoticeCtrlInst.GetNoticeById)
	noticePathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("system:notice:add"), sysNoticeCtrlInst.AddNotice)...)
	noticePathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("system:notice:edit"), sysNoticeCtrlInst.UpdateNotice)...)
	noticePathGroup.DELETE("/:id", r.mergeHandlers(extraHandlers, r.checkPermission("system:notice:remove"), sysNoticeCtrlInst.DeleteNotices)...)
}

// tool
func (r *RouterMakeup) makeToolPathGroup(privateGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	toolPathGroup := r.createRouterGroup(ginProperty.Router.ToolGroupPath, "/tool", privateGroup)
	r.makeToolCodeGenPathGroup(toolPathGroup, ginProperty)
}

// code gen
func (r *RouterMakeup) makeToolCodeGenPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	codeGenCtrlInst := http.MainCtrlGroupInst.ToolCtrlGroupInst.CodeGenCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), codeGenCtrlInst.InitCtrl(codeGenCtrlInst)}
	codeGenPathGroup := r.createRouterGroup("/codegen", "/codegen", contextPathGroup)

	codeGenPathGroup.GET("/:bizId/list", r.checkPermission("tool:gen:list"), codeGenCtrlInst.GetGenTablePage)
	codeGenPathGroup.GET("/:bizId/db/list", r.checkPermission("tool:gen:list"), codeGenCtrlInst.GetDbTableList)
	codeGenPathGroup.POST("/:bizId/saveImportTable", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:import"), codeGenCtrlInst.SaveImportTable)...)
	codeGenPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:edit"), codeGenCtrlInst.UpdateCodeGen)...)
	codeGenPathGroup.DELETE("/:ids", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:remove"), codeGenCtrlInst.DeleteCodeGenByIds)...)
	codeGenPathGroup.GET("/:bizId/:id", r.checkPermission("tool:gen:query"), codeGenCtrlInst.GetCodeGenDetail)
	codeGenPathGroup.GET("/:bizId/downloadByTable/:tableName", r.checkPermission("tool:gen:code"), codeGenCtrlInst.DownloadByTable)
	codeGenPathGroup.GET("/:bizId/batchGenCode", r.checkPermission("tool:gen:code"), codeGenCtrlInst.BatchGenCode)
	codeGenPathGroup.POST("/:bizId/syncDb", r.checkPermission("tool:gen:import"), codeGenCtrlInst.SyncDb)
	r.makeToolCodeGenBizPathGroup(codeGenPathGroup, ginProperty)
	r.makeToolCodeGenClassifyPathGroup(codeGenPathGroup, ginProperty)
	r.makeToolCodeGenDataSourcePathGroup(codeGenPathGroup, ginProperty)
}

// codegen biz
func (r *RouterMakeup) makeToolCodeGenBizPathGroup(codeGenPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	//gen biz
	genBizCtrlInst := http.MainCtrlGroupInst.ToolCtrlGroupInst.GenBizCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), genBizCtrlInst.InitCtrl(genBizCtrlInst)}
	genGizPathGroup := r.createRouterGroup("/biz", "/biz", codeGenPathGroup)
	genGizPathGroup.GET("/list", r.checkPermission("tool:gen:biz:list"), genBizCtrlInst.GetGenBizPage)
	genGizPathGroup.GET("/:id", r.checkPermission("tool:gen:biz:query"), genBizCtrlInst.GetGenBizById)
	genGizPathGroup.GET("/", r.checkPermission("tool:gen:biz:query"), genBizCtrlInst.GetGenBizById)
	genGizPathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:biz:add"), genBizCtrlInst.AddGenBiz)...)
	genGizPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:biz:edit"), genBizCtrlInst.UpdateGenBizById)...)
	genGizPathGroup.DELETE("/:ids", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:biz:remove"), genBizCtrlInst.DeleteGenBizByIds)...)
	genGizPathGroup.PUT("/changeStatus", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:biz:edit"), genBizCtrlInst.ChangeStatus)...)
}

// classify
func (r *RouterMakeup) makeToolCodeGenClassifyPathGroup(codeGenPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	genTemplateClassifyCtrlInst := http.MainCtrlGroupInst.ToolCtrlGroupInst.GenTemplateClassifyCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), genTemplateClassifyCtrlInst.InitCtrl(genTemplateClassifyCtrlInst)}
	genTemplateClassifyPathGroup := r.createRouterGroup("/classify", "/classify", codeGenPathGroup)
	genTemplateClassifyPathGroup.GET("/getAll", r.checkPermission("tool:gen:classify:list"), genTemplateClassifyCtrlInst.GetAllClassifys)
	genTemplateClassifyPathGroup.GET("/list", r.checkPermission("tool:gen:classify:list"), genTemplateClassifyCtrlInst.GetClassifyPage)
	genTemplateClassifyPathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:classify:add"), genTemplateClassifyCtrlInst.AddClassify)...)
	genTemplateClassifyPathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:classify:edit"), genTemplateClassifyCtrlInst.UpdateClassify)...)
	genTemplateClassifyPathGroup.DELETE("/:ids", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:classify:remove"), genTemplateClassifyCtrlInst.DeleteClassify)...)
	genTemplateClassifyPathGroup.GET("/:id", r.checkPermission("tool:gen:classify:query"), genTemplateClassifyCtrlInst.GetClassifyById)
	genTemplateClassifyPathGroup.GET("/detect", r.checkPermission("tool:gen:classify:detect"), genTemplateClassifyCtrlInst.DetectTemplateFiles)
	genTemplateClassifyPathGroup.PUT("/changeStatus", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:classify:edit"), genTemplateClassifyCtrlInst.ChangeStatus)...)
}

// datasource
func (r *RouterMakeup) makeToolCodeGenDataSourcePathGroup(codeGenPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	genDatasourceCtrlInst := http.MainCtrlGroupInst.ToolCtrlGroupInst.GenDatasourceCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), genDatasourceCtrlInst.InitCtrl(genDatasourceCtrlInst)}
	genDatasourcePathGroup := r.createRouterGroup("/datasource", "/datasource", codeGenPathGroup)
	genDatasourcePathGroup.GET("/list", r.checkPermission("tool:gen:datasource:list"), genDatasourceCtrlInst.GetDatasourcePage)
	genDatasourcePathGroup.GET("/:id", r.checkPermission("tool:gen:datasource:query"), genDatasourceCtrlInst.GetDatasourceById)
	genDatasourcePathGroup.GET("/", r.checkPermission("tool:gen:datasource:query"), genDatasourceCtrlInst.GetDatasourceById)
	genDatasourcePathGroup.POST("", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:datasource:add"), genDatasourceCtrlInst.AddDatasource)...)
	genDatasourcePathGroup.PUT("", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:datasource:edit"), genDatasourceCtrlInst.UpdateDatasource)...)
	genDatasourcePathGroup.DELETE("/:ids", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:datasource:remove"), genDatasourceCtrlInst.DeleteDatasource)...)
	genDatasourcePathGroup.PUT("/changeStatus", r.mergeHandlers(extraHandlers, r.checkPermission("tool:gen:datasource:edit"), genDatasourceCtrlInst.ChangeStatus)...)

	genDatasourcePathGroup.GET("/testConnection", r.checkPermission("tool:gen:datasource:query"), genDatasourceCtrlInst.TestConnection)
}

// monitor
// oper log
func (r *RouterMakeup) makeMonitorOperLogPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysOperLogCtrlInst := http.MainCtrlGroupInst.MonitorCtrlGroupInst.SysOperLogCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysOperLogCtrlInst.InitCtrl(sysOperLogCtrlInst)}
	operLogPathGroup := r.createRouterGroup("/operlog", "/operlog", contextPathGroup)
	operLogPathGroup.GET("/list", r.checkPermission("monitor:operlog:list"), sysOperLogCtrlInst.GetOperLogPage)
	operLogPathGroup.DELETE("/:ids", r.mergeHandlers(extraHandlers, r.checkPermission("monitor:operlog:remove"), sysOperLogCtrlInst.DeleteOperLogs)...)
	operLogPathGroup.DELETE("/clean", r.mergeHandlers(extraHandlers, r.checkPermission("monitor:operlog:remove"), sysOperLogCtrlInst.CleanTable)...)
	operLogPathGroup.POST("/export", r.mergeHandlers(extraHandlers, r.checkPermission("monitor:operlog:export"), sysOperLogCtrlInst.ExportOperLogs)...)
}

// login log info
func (r *RouterMakeup) makeMonitorLoginInfoPathGroup(contextPathGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	sysLoginInfoCtrlInst := http.MainCtrlGroupInst.MonitorCtrlGroupInst.SysLoginInfoCtrlInst
	extraHandlers := []gin.HandlerFunc{loginfo.DoLogOperInfo(), sysLoginInfoCtrlInst.InitCtrl(sysLoginInfoCtrlInst)}
	loginLogPathGroup := r.createRouterGroup("/logininfor", "/logininfor", contextPathGroup)
	loginLogPathGroup.GET("/list", r.checkPermission("monitor:logininfor:list"), sysLoginInfoCtrlInst.GetLoginInfoPage)
	loginLogPathGroup.DELETE("/:ids", r.mergeHandlers(extraHandlers, r.checkPermission("monitor:logininfor:remove"), sysLoginInfoCtrlInst.DeleteLoginInfos)...)
	loginLogPathGroup.DELETE("/clean", r.mergeHandlers(extraHandlers, r.checkPermission("monitor:logininfor:remove"), sysLoginInfoCtrlInst.CleanTable)...)
	loginLogPathGroup.POST("/export", r.mergeHandlers(extraHandlers, r.checkPermission("monitor:logininfor:export"), sysLoginInfoCtrlInst.ExportLoginInfos)...)
}

func (r *RouterMakeup) makeStatic(publicGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	for mapKey, mapValue := range ginProperty.Router.StaticMap {
		if publicGroup != nil {
			publicGroup.Static(mapKey, mapValue)
		} else {
			framework.GinRouter.Static(mapKey, mapValue)
		}
	}
	for mapKey, mapValue := range ginProperty.Router.StaticFileMap {
		if publicGroup != nil {
			publicGroup.StaticFile(mapKey, mapValue)
		} else {
			framework.GinRouter.StaticFile(mapKey, mapValue)
		}
	}
}

func (r *RouterMakeup) checkPermission(permission string) gin.HandlerFunc {
	return authorization.CheckPermission(permission, r.authSubject)
}
func (r *RouterMakeup) makeOtherRouter(publicGroup *gin.RouterGroup, ginProperty *properties_gin.GinProperty) {
	if framework.ConfigContainerInst.BootstrapProperty.Sys.Profile.Mode != consts.ReleaseMode {
		if publicGroup != nil {
			publicGroup.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
		} else {
			framework.GinRouter.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
		}
	}
}

func (r *RouterMakeup) createRouterGroup(configPath, defaultPath string,
	contextPathGroup *gin.RouterGroup, handlers ...gin.HandlerFunc) *gin.RouterGroup {
	if configPath == "" {
		configPath = defaultPath
	}
	var pathGroup *gin.RouterGroup
	if contextPathGroup != nil {
		pathGroup = contextPathGroup.Group(configPath, handlers...)
	} else {
		pathGroup = framework.GinRouter.Group(configPath, handlers...)
	}
	return pathGroup
}

func (r *RouterMakeup) mergeHandlers(arrayHandles []gin.HandlerFunc, handlers ...gin.HandlerFunc) gin.HandlersChain {
	ret := gin.HandlersChain{}
	ret = append(ret, arrayHandles...)
	ret = append(ret, handlers...)
	return ret
}
